app.controller('ordersRefundCtrl',['$scope','$modal','refundService','utilService','toaster','$filter',
    function($scope,$modal,refundService,utilService,toaster,$filter){

        $scope.ordersRefund={};
        $scope.query = function(){
            $scope.ordersRefund.startTime = $filter("date")($scope.ordersRefund.startTime, "yyyy-MM-dd");
            $scope.ordersRefund.endTime = $filter("date")($scope.ordersRefund.endTime, "yyyy-MM-dd");
            utilService.queryForPagingList($scope,{
                url:$scope.app.domain + 'orders/refund',
                params:$scope.ordersRefund
            }).then(function(result){
            });
        };
        $scope.query();

        //获得店铺列表
        $scope.getShopList = function(){
            $scope.shopSelectList=[];
            refundService.getShopList($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.shopSelectList = result.data;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.getShopList();

        //同意退款
        $scope.updateStatus = function(id){
            var param = {
                id:id
            };
            openModal("您确定同意该商品退款吗？");

            function openModal(msg){
                utilService.modal($modal, $scope, {
                    title:"温馨提示",
                    controller:"updateCtrl",
                    message:msg,
                    data:param
                });
            }
        };
    }
]);

app.controller('goodsRefundCtrl',['$scope','$modal','refundService','utilService','toaster','$stateParams',
    function($scope,$modal,refundService,utilService,toaster,$stateParams){

        $scope.goodsRefundVO={
            id:$stateParams.id
        };
        $scope.query = function(){
            refundService.query($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.goodsRefund=result.data;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            })
        };
        $scope.query();
    }
]);

//同意退款模态框
app.controller('updateCtrl', ['$scope', '$modalInstance','refundService','toaster','modalData',
    function($scope, $modalInstance,refundService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.ordersRefundVO = {
            id: modalData.data.id
        };
        $scope.isDelModal=true;
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            refundService.agree($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        $scope.query();
                        break;
                    case 714:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "退款失败！");
                        $scope.isDelModal=true;
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            })
        };
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);

//拒绝退款
app.controller('rejectOrdersRefundCtrl',['$scope','$state','refundService','toaster','$stateParams',
    function($scope,$state,refundService,toaster,$stateParams){
    $scope.ordersRefundVO={
        id:$stateParams.id,
        status:4
    };

    $scope.confirm = function(){
        refundService.update($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.ordersRefund');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };

}]);